package com.link.base.base.address.service;

import com.link.base.base.address.dao.mybatis.mapper.AddrPstMapper;
import com.link.base.base.address.model.AddrPst;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.AppConstants;
import com.link.core.util.JsonUtil;
import com.link.core.util.RedisUtil;
import com.link.core.util.StringUtils;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/8 10:21
 * @version 1.0
 */

@Service
public class AddrPstServiceImpl extends BasicServiceImpl<AddrPst> implements AddrPstService {

    @Resource
    private AddrPstMapper addrPstMapper;

    @Override
    public BasicMapper<AddrPst> getBasicMapper() {
        return addrPstMapper;
    }

    /**
     * 查询下级地址
     * @param qps
     * @return List
     * @throws BasicServiceException
     */
    @Override
    public List<AddrPst> queryChildAddrListSelect(QueryParams qps) throws BasicServiceException {
        List<AddrPst> list = null;
        try {
            list = addrPstMapper.queryChildAddrListSelect(qps);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new BasicServiceException(e.getMessage());
        }
        return list;
    }

    /**
     * 查询所有
     * @param record
     * @return List
     * @throws BasicServiceException
     */
    @Override
    public List<AddrPst> queryByExample(AddrPst record) throws BasicServiceException {
        List<AddrPst> list = null;
        try {
            list = addrPstMapper.queryByExample(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new BasicServiceException(e.getMessage());
        }
        return list;
    }

    /**
     * redis刷新
     * @param forceRefresh 为true则强制刷新
     * @param corpId       账套id
     * @throws Exception
     */
    @Override
    public void cacheAddrPst(boolean forceRefresh, Long corpId) throws Exception {
        LinkRedisClient j = RedisUtil.getJedis();
        String redisKey = ADDR_CACHE_KEY;
        AddrPst example = new AddrPst();
        example.setLoginCorpId(corpId);
        example.setPrivateAddr("N");
        example.setActive("Y");
        if (!AppConstants.ADMIN_CORP_ID.equals(corpId)) {
            redisKey = ADDR_CACHE_KEY + "_" + corpId;
            example.setPrivateAddr("Y");
        }
        try {
            if (!j.exists(redisKey) || forceRefresh) {
                //缓存里没有数据或者强制刷新标志为true则重新查询数据并写到缓存里
                List<AddrPst> list = addrPstMapper.queryAll(example);
                Map<String, List<AddrPst>> map = new HashMap<String, List<AddrPst>>(16);
                for (int i = 0; i < list.size(); i++) {
                    AddrPst addr = list.get(i);
                    String parentName = addr.getParentAddrName() == null ? "" : addr.getParentAddrName();
                    String parent2Name = "";
                    String type = addr.getAddrType();
                    String key = parent2Name + parentName;
                    String typeKey = type;
                    //根据父级地址名称分类
                    List<AddrPst> list1 = map.get(key);
                    //根据地址类型分类
                    List<AddrPst> typeList = map.get(typeKey);
                    if (list1 == null) {
                        list1 = new ArrayList<AddrPst>();
                        map.put(key, list1);
                    }
                    if (typeList == null) {
                        typeList = new ArrayList<AddrPst>();
                        map.put(typeKey, typeList);
                    }
                    list1.add(addr);
                    typeList.add(addr);
                }
                Map<String, String> strMap = new HashMap<>(16);
                for (Map.Entry<String, List<AddrPst>> entry : map.entrySet()) {
                    if (!StringUtils.isBlank(entry.getKey())) {
                        strMap.put(entry.getKey(), JsonUtil.toString(entry.getValue()));
                    }
                }
                j.hmset(redisKey, strMap);
            }
        } catch (Exception e) {
            LCLogger.withException(e);
            throw e;
        } finally {
            RedisUtil.returnResource(j);
        }
    }
}
